//路线表
const express=require('express')
const router=express.Router()
const pool=require('../utils/db')
const Response=require('../utils/Response')

//cyss_line
//查询所有线路 --后台管理走的接口
router.get('/line-list',(req,res)=>{
    let {page,pagesize} = req.query

    let startIndex= (page - 1) * 10
    let size= parseInt(pagesize)
    let sql ="select * from cyss_line limit ?,?"
    pool.query(sql,[startIndex,size],(error,result)=>{
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})

//模糊查询  --后台,前台都走的接口
router.post('/line-name',(req,res)=>{
    let {name} =req.body
    let sql="select * from cyss_line where line_journey like ?"
    pool.query(sql,[`%${name}%`],(error,result)=>{
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})

//按ID 查询线路信息，装填到页面中 
router.post('/lineId',(req,res) => {
    let id = req.body.id
    // console.log(req.body.id)
    let sql = "select * from cyss_line where line_id=?"
    pool.query(sql,[id],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})

//添加线路 --后台管理
router.post('/line-add',(req,res)=>{
    let {line_name,line_price,line_journey}=req.body
    
    let sql="insert into cyss_line (line_name,line_price,line_journey) values(?,?,?)"
    pool.query(sql,[line_name,line_price,line_journey],(error,result)=>{
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok())
    })
})

// 修改线路
router.post('/line-edit',(req,res) => {
    let {line_name,line_price,line_journey,line_id} = req.body

    let sql = "update cyss_line set line_name=?,line_price=?,line_journey=? where line_id=?"
    pool.query(sql,[line_name,line_price,line_journey,line_id],(error,result) => {
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok())
    })
})

//删除线路
router.post('/line-del',(req,res)=>{
    let {id}=req.body
    
    let sql="delete from cyss_line where line_id=?"
    pool.query(sql,[id],(error,result)=>{
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok())
    })
})
//导出路由对象
module.exports=router